//
//  KTWebSocket.swift
//  KOLToken
//
//  Created by san.yang on 2020/3/5.
//  Copyright © 2020 iOS-team. All rights reserved.
//

import UIKit
import Starscream
@objc public protocol KTWebSocketDelegate: NSObjectProtocol{
    /**websocket 连接成功*/
    @objc optional func websocketDidConnect(sock: KTWebSocket)
    /**websocket 连接失败*/
    @objc optional  func websocketDidDisconnect(socket: KTWebSocket, error: NSError?)
    /**websocket 接受文字信息*/
    func websocketDidReceiveMessage(socket: KTWebSocket, text: String)
    /*
     *websocket 接受二进制信息
     */
    @objc optional  func  websocketDidReceiveData(socket: KTWebSocket, data: NSData)
}
public class KTWebSocket: NSObject,WebSocketDelegate {
    public func didReceive(event: WebSocketEvent, client: WebSocket) {
        KTLog("sad")
    }
    
    var socket:WebSocket!
    weak var webSocketDelegate: KTWebSocketDelegate?
    //单例
    class func sharedInstance() -> KTWebSocket
    {
        return manger
    }
    static let manger: KTWebSocket = {
        return KTWebSocket()
    }()
    
    //MARK:- 链接服务器
    func connectSever(urlString: String){
        socket = WebSocket(request: URLRequest(url: URL(string: "wss://api.huobi.pro/ws")!))
        socket.delegate = self
        socket.connect()
    }
    
    //MARK:- 关闭消息
    func disconnect(){
        socket.disconnect()
    }
    
    //MARK: - WebSocketDelegate
    //客户端连接到服务器时，websocketDidConnect将被调用。
    public func websocketDidConnect(socket: WebSocket){
        
        webSocketDelegate?.websocketDidConnect!(sock: self)
    }
    //客户端与服务器断开连接后，将立即调用 websocketDidDisconnect。
    public func websocketDidDisconnect(socket: WebSocket, error: NSError?){
        debugPrint("连接失败了: \(String(describing: error?.localizedDescription))")
        webSocketDelegate?.websocketDidDisconnect!(socket: self, error: error)
    }
    //当客户端从连接获取一个文本框时，调用 websocketDidReceiveMessage。
    //注：一般返回的都是字符串
    public func websocketDidReceiveMessage(socket: WebSocket, text: String){
        
        webSocketDelegate?.websocketDidReceiveMessage(socket: self, text: text)
    }
    public func websocketDidReceiveData(socket: WebSocket, data: NSData){
        debugPrint("data数据")
        webSocketDelegate?.websocketDidReceiveData!(socket: self, data: data)
    }
    
}
//let webSocket = WebSocketSingle.shared
//
//final class WebSocketSingle:NSObject,WebSocketDelegate {
//    func didReceive(event: WebSocketEvent, client: WebSocket) {
//
//    }
//
//
////  socket连接上函数
//    func websocketDidConnect(socket: WebSocketClient) {
//    }
//
////  socket断开执行函数
//    func websocketDidDisconnect(socket: WebSocketClient, error: Error?) {
//    }
//
////  接收返回消息函数
//    func websocketDidReceiveMessage(socket: WebSocketClient, text: String) {
//    }
//
//    func websocketDidReceiveData(socket: WebSocketClient, data: Data) {
//    }
//
//    //构造单例数据
//    static let shared = WebSocketSingle()
//    private override init() {
//    }
//}
//
//// initSocket方法
//func initWebSocketSingle () {
//        SingletonSocket.sharedInstance.socket.delegate = webSocket
//}
//
////声明webSocket单例
//class SingletonSocket {
//    //socketURL为websocket地址
//    let socket:WebSocket = WebSocket(url: URL(string: socketURL)!)
//
//    class var sharedInstance : SingletonSocket{
//        struct Static{
//            static let instance:SingletonSocket = SingletonSocket()
//        }
//
//        if !Static.instance.socket.isConnected{
//            Static.instance.socket.connect()
//        }
//        return Static.instance
//    }
//}
